require 'rexml/document'
include REXML

class SearchResponse
  
  attr_accessor :request_id, :query_images, :collection_id, :skin_color_weight, :hair_weight, :recognition_mode, :profile_code, :use_cache, :algorithm, :search_results
  
  def initialize()
    
  end
  
  def self.element_value(node, expression)
    
    elements = node.elements[expression]
    
    if (elements != nil) 
      return elements[0].to_s()
    else
      return nil
    end
    
  end
  
  def self.fromXml(doc) 
   
    r = SearchResponse.new()
    r.request_id = element_value(doc, "search-response/request-id")
    
    
    r.query_images = []
    
    i = 0
    
    doc.elements.each("search-response/query-image") do |imageNode|
    image = QueryImage.new();
      image.roi_id = element_value(imageNode, "roi-id")
      image.weight = element_value(imageNode, "weight")
      
      r.query_images[i] = image
      i = i + 1
      
    end
    
    
    r.collection_id = element_value(doc, "search-response/collection-id")
    r.skin_color_weight = element_value(doc, "search-response/skin-color-weight")
    r.hair_weight = element_value(doc, "search-response/hair-weight")
    r.recognition_mode = element_value(doc, "search-response/recognition-mode")
    r.profile_code = element_value(doc, "search-response/profile-code")
    r.use_cache = element_value(doc, "search-response/use-cache")
    r.algorithm = element_value(doc, "algorithm")

    i = 0
    
    r.search_results = []
    
    doc.elements.each("search-response/search-result") do |resultNode|
      result = SearchResult.new();
      result.rank = element_value(resultNode, "rank")
      result.roi_id = element_value(resultNode, "roi-id")
      result.bounding_box = element_value(resultNode, "bounding-box")
      result.score = element_value(resultNode, "score")
      result.direct_comparison_score = element_value(resultNode, "direct-comparison-score")
      result.image_url = element_value(resultNode, "image-url")
      result.image_id = element_value(resultNode, "image-id")
      result.entity_id = element_value(resultNode, "entity_id")
      result.entity_name = element_value(resultNode, "entity-name")
      result.description = element_value(resultNode, "description")
      
      r.search_results[i] = result
      i = i + 1
      
    end

 
    return r
    
  end
  
  class SearchResult
    
    attr_accessor :rank, :roi_id, :bounding_box, :score, :direct_comparison_score, :image_url, :image_id, :entity_Id, :entity_name, :description
    
    def initialize()
      
    end

    
  end
  
  class QueryImage
    
    attr_accessor :roi_id, :weight
    
    def initialize()
      
    end

  end
  
end